// pages/User/User.js
const app=getApp();
Page({

 
  data: {
     
    userInfo:{},
    Uname: "",
    showImage:false,
    Uimage:"",
    Uimage2:"https://s3.bmp.ovh/imgs/2022/04/26/91a70111a94c67a7.png",
    Uid:'',

    
    is_modal_Hidden:false,  
    is_modal_Msg:'我是一个自定义组件'  
  },

  onShareAppMessage() {
    return {
          title: '弹出分享时显示的分享标题',
          desc: '分享页面的内容',
          // path: '../User' // 路径，传递参数到指定页面。
 
     }
 
 },

  /* 分享小程序 */ 
  aboutUs:function() {
    wx.navigateTo({
      url: '../User-aboutUs/User-aboutUs',
    })

  },

/* 跳转我的收藏 */
  jumpCollect: function(e){
    if(wx.getStorageSync('uid')){
      wx.navigateTo({ /* ?Wid= "+petsnum */
        url:"/pages/User-collect/User-collect"
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '登录后收藏',
        showCancel: true, 
        cancelText: "不登录", 
        cancelColor: ' rgb(178, 89, 75)', 
        confirmText: "去登录", 
        confirmColor: 'rgb(178, 89, 75)', 
        success: function(res) {
          if (res.cancel) {
         
           
          } else if(res.confirm){
             
          }
        }
      })
    }
    
  },
  jumpFeed:function(){ //跳转意见反馈
    if(wx.getStorageSync('uid')){
      wx.navigateTo({ /* ?Wid= "+petsnum */
        url:"/pages/User-feedback/User-feedback"
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '登录后反馈',
        showCancel: true, 
        cancelText: "不登录", 
        cancelColor: ' rgb(178, 89, 75)', 
        confirmText: "去登录", 
        confirmColor: 'rgb(178, 89, 75)', 
        success: function(res) {
          if (res.cancel) {
         
           
          } else if(res.confirm){
             
          }
        }
      })
    }
    
    
  },
   /* 退出登录 */
   exitLogin:function(e){
    if(wx.getStorageSync('uid')){
       console.log('-------',this.modal.data.showModal)
      if(!this.modal.data.showModal) {
        if(!this.modal.data.cancel){
          console.log('点击确定键',this.modal.data.cancel)
          wx.removeStorageSync('Uimage');
          wx.removeStorageSync('Uname'); 
          wx.removeStorageSync('uid'); 
          this.setData({
            Uimage:wx.getStorageSync('Uimage'),
            showImage:false
          })
        }
        else{
          console.log('点击取消键')
        }
        
      }
    }
    else{
       
    }
    
     
    /* wx.showModal({
      title: '提示',
      content: '确定退出？',
      showCancel: true, 
      cancelText: "取消", 
      cancelColor: ' rgb(178, 89, 75)', 
      confirmText: "确定", 
      confirmColor: 'rgb(178, 89, 75)', 
      success: function(res) {
        if (res.cancel) {
       
         
        } else if(res.confirm){
          
         wx.clearStorageSync();
         if(!wx.getStorageSync('Uimage')){
          console.log("Uimage:",wx.getStorageSync('Uimage'));
          
          console.log(that.data)
          that.setData({
            Uimage:wx.getStorageSync('Uimage'),
            showImage:false
        })
        }
           
          
        }
      }
    }) */
  },

  getAuthorize:function(e){
    var that = this; 
    if(wx.getStorageSync('uid')){  /* 已经登陆 */
      console.log("已经登陆")
       
    }
    else{
        wx.getUserProfile({
          desc:'必须授权才可以继续使用',
          success:res=>{
              console.log('授权成功',res)
              that.setData({
                Uimage:res.userInfo.avatarUrl,
                Uname:res.userInfo.nickName,
                showImage:true
              })
              console.log(res.userInfo)
              this.uploadData(res.userInfo.avatarUrl,res.userInfo.nickName);
              wx.setStorageSync('Uname',res.userInfo.nickName);
              wx.setStorageSync('Uimage',res.userInfo.avatarUrl);
  
          },
          fail:res=>{
              console.log('授权失败',res)
          }
      })  
      
      //获取用户唯一openId
      wx.login({
          success: res => {
              if (res.code) {
                console.log(res.code)
                /* https://api.weixin.qq.com/sns/jscode2session?appid=wx44335182bf757988&secret=e8f101033b4236755b648dbbbfaefbd7&js_code=' + res.code + '&grant_type=authorization_code */
                
                wx.request({ 
                      url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx44335182bf757988&secret=d8bb64421d58c36f71e55cc255b2da0d&js_code='+res.code+'&grant_type=authorization_code',
                      data: {},
                      header: {
                          'content-type': 'json'
                      },
                      success: function (res) {
                          console.log("login====",res);
                          console.log('openid为' + res.data.openid);
                          that.setData({
                              Uid:res.data.openid
                          })
                          wx.setStorageSync('uid',res.data.openid);
                      
                      }
                  })
              } else {
                  console.log('登录失败' + res.errMsg)
              }
          }
      })
    
    }
    
  },
  uploadData:function(uimage,uname) {   /* 将用户信息上传到数据库   */
    var that = this;
    let value = this.data.userInfo;   
    wx.request({
      url: `${app.globalData.url}/users/`,//写自己的服务器
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      data: {/* that.data.Uid */
        uid:that.data.Uid,
        uname:uname,
        uimage:uimage,
        ufeedback:"111"
        
      },
      success: function (res) { 
        console.log(res) ;
        // wx.setStorage({
        //     data:that.data.Uid,
        //     key:'Uid'
        // })
        // wx.setStorage({
        //   data: uimage,
        //   key: 'Uimage',
        // })
      },
      fail: function (res) {
        console.log("fail")
        console.log(res)          
      }

  })



},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow')
    if(wx.getStorageSync('Uimage')){
      console.log("Uimage:",wx.getStorageSync('Uimage'));
      console.log(this.data.showImage);

      this.setData({
        Uimage:wx.getStorageSync('Uimage'),
        Uname:wx.getStorageSync('Uname'),
        showImage:true
    }) 
  }
    // console.log(this.data.festival)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})